package mytool;

/**
 * Created by zerdoor_pc on 2016/3/15.
 */

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * GPS坐标转换为百度地图坐标
 * 需要引入javabase64.jar 和json的一些包
 *
 */
public class Gps2BaiDu {
    public static LatLng to(LatLng sourceLatLng){
        // 将GPS设备采集的原始GPS坐标转换成百度坐标
        CoordinateConverter converter = new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待转换坐标
        converter.coord(sourceLatLng);
        LatLng desLatLng = converter.convert();

        return desLatLng;
    }



    /// <summary>
    /// 数字经纬度和度分秒经纬度转换 (Digital degree of latitude and longitude and vehicle to latitude and longitude conversion)
    /// </summary>
    /// <param name="digitalLati_Longi">数字经纬度</param>
    /// <return>度分秒经纬度</return>
    static public String ConvertDigitalToDegrees(String digitalLati_Longi)
    {
        double digitalDegree = Double.parseDouble(digitalLati_Longi);
        return ConvertDigitalToDegrees(digitalDegree);
    }

    /// <summary>
    /// 数字经纬度和度分秒经纬度转换 (Digital degree of latitude and longitude and vehicle to latitude and longitude conversion)
    /// </summary>
    /// <param name="digitalDegree">数字经纬度</param>
    /// <return>度分秒经纬度</return>
    static public String ConvertDigitalToDegrees(double digitalDegree)
    {
          double num = 60;
        int degree = (int)digitalDegree;
        double tmp = (digitalDegree - degree) * num;
        int minute = (int)tmp;
        double second = (tmp - minute) * num;
        String degrees = "" + degree + "°" + minute + "′" + second + "″";
        return degrees;
    }


    /// <summary>
    /// 度分秒经纬度(必须含有'°')和数字经纬度转换
    /// </summary>
    /// <param name="digitalDegree">度分秒经纬度</param>
    /// <return>数字经纬度</return>
    static public double ConvertDegreesToDigital(String degrees)
    {
          double num = 60;
        double digitalDegree = 0.0;
        int d = degrees.indexOf('°');           //度的符号对应的 Unicode 代码为：00B0[1]（六十进制），显示为°。
        if (d < 0)
        {
            return digitalDegree;
        }
        String degree = degrees.substring(0, d);
        digitalDegree += Double.parseDouble(degree);

        int m = degrees.indexOf('′');           //分的符号对应的 Unicode 代码为：2032[1]（六十进制），显示为′。
        if (m < 0)
        {
            return digitalDegree;
        }
        PrintLog.i("degree="+degrees);
        PrintLog.i("degree=d="+d);
        PrintLog.i("degree=m="+m);
        String minute = degrees.substring(d + 1, m);
        digitalDegree += ((Double.parseDouble(minute)) / num);

        int s = degrees.indexOf('″');           //秒的符号对应的 Unicode 代码为：2033[1]（六十进制），显示为″。
        if (s < 0)
        {
            return digitalDegree;
        }
        String second = degrees.substring(m + 1, s );
        digitalDegree += (Double.parseDouble(second) / (num * num));

        return digitalDegree;
    }


    /// <summary>
    /// 度分秒经纬度(必须含有'/')和数字经纬度转换
    /// </summary>
    /// <param name="digitalDegree">度分秒经纬度</param>
    /// <param name="cflag">分隔符</param>
    /// <return>数字经纬度</return>
    static public double ConvertDegreesToDigital_default(String degrees)
    {
        char ch = '/';
        return ConvertDegreesToDigital(degrees, ch);
    }

    /// <summary>
    /// 度分秒经纬度和数字经纬度转换
    /// </summary>
    /// <param name="digitalDegree">度分秒经纬度</param>
    /// <param name="cflag">分隔符</param>
    /// <return>数字经纬度</return>
    static public double ConvertDegreesToDigital(String degrees, char cflag)
    {
          double num = 60;
        double digitalDegree = 0.0;
        int d = degrees.indexOf(cflag);
        if (d < 0)
        {
            return digitalDegree;
        }
        String degree = degrees.substring(0, d);
        digitalDegree += Double.parseDouble(degree);

        int m = degrees.indexOf(cflag, d + 1);
        if (m < 0)
        {
            return digitalDegree;
        }
        String minute = degrees.substring(d + 1, m - d - 1);
        digitalDegree += ((Double.parseDouble(minute)) / num);

        int s = degrees.length();
        if (s < 0)
        {
            return digitalDegree;
        }
        String second = degrees.substring(m + 1, s - m - 1);
        digitalDegree += (Double.parseDouble(second) / (num * num));

        return digitalDegree;
    }
}
